#pragma once
#include <wininet.h>

/*
custom errorcodes:
-1: bad Url...
*/

class CGrabHttp
{
public:
	CGrabHttp(void);
	~CGrabHttp(void);

	//get a page, if post is false, HTTP GET is used othervise HTTP POST is used. 
	//if PostDataLength is -1 the data must be NULL terminated...
	CString GetPage(int iIndexThread, LPCTSTR strUrl = NULL, bool Post = false, LPCTSTR strAgentName = NULL,LPCSTR PostData = NULL, size_t PostDataLength = -1);


private:
	//get a specific line from the headers
	LPTSTR GetHeaderLine(LPTSTR s, LPTSTR Headers =NULL);
	//get the HTTP statuscode for the last received page
	int GetPageStatusCode(LPTSTR Headers =NULL); 
	//chack header top line,if it is 200 ok ,request is success.
	bool CheckHeader200(HINTERNET hInternetRequest);
	void TraceAPIErr(DWORD  LastError);

	HINTERNET m_hInternetSession;
	HINTERNET m_hInternetConnect[512];

};

typedef struct 
{
	HINTERNET	hInternetRequest;
	HINTERNET	hInternetConnect;
	HINTERNET	hInternetSession;
	LPCTSTR		strUrl;
	BOOL		bSendRequest;
	bool		bPost ;
	LPCSTR PostData;
	size_t PostDataLength;
} GETPARM;

DWORD WINAPI WorkerFunction(IN LPVOID vThreadParm );
void CrackUrl(LPCTSTR strUrl, URL_COMPONENTS& uc);
